/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：Manager                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Services                                  
*│　类    名： ManagerService                                    
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.Common;
using NetCoreCMS.IRepository;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NetCoreCMS.Services
{
    public class ManagerService: IManagerService
    {
        private readonly IManagerRepository _repository;

        public ManagerService(IManagerRepository repository)
        {
            _repository = repository;
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            var model = _repository.Get(id);
            return model != null ? true : false;
        }
        /// <summary>
        /// 查询用户名是否存在
        /// </summary>
        public bool Exists(string user_name)
        {
            string conditions = "select * from [Manager] where user_name=@user_name ";
            var model = _repository.Get(conditions, new { user_name = user_name });
            return model != null ? true : false;
        }

        /// <summary>
        /// 根据用户名取得Salt
        /// </summary>
        public string GetSalt(string user_name)
        {
            string conditions = "select * from [Manager] where user_name=@user_name ";
            var model = _repository.Get(conditions, new { user_name = user_name });
            return model!=null?model.salt:"";
        }

        /// <summary>
        /// 根据用户名密码返回一个实体
        /// </summary>
        public Models.Manager GetModel(string user_name, string password)
        {
            string conditions = "select * from [Manager] where user_name=@user_name and password=@password and is_lock=0 ";
            var model = _repository.Get(conditions, new { user_name = user_name, password = password });
            return model;
        }

        /// <summary>
        /// 根据用户名密码返回一个实体
        /// </summary>
        public Models.Manager GetModel(string user_name, string password, bool is_encrypt)
        {
            //检查一下是否需要加密
            if (is_encrypt)
            {
                //先取得该用户的随机密钥
                string salt = GetSalt(user_name);
                if (string.IsNullOrEmpty(salt))
                {
                    return null;
                }
                //把明文进行加密重新赋值
                password = AESEncryptHelper.Encode(password, salt);
            }
            return GetModel(user_name, password);
        }

        public Manager GetModel(int id)
        {
            return _repository.Get(id);
        }

        public int Add(Manager model)
        {
            return (int)_repository.Insert(model);
        }

        public bool Update(Manager model)
        {
            return _repository.Update(model)>0;
        }

        public bool Delete(int id)
        {
            return _repository.Delete(id) > 0;
        }

        public List<Manager> GetList(int pageNumber, int rowsPerPage, string conditions, string orderby, object parameters = null)
        {
            return _repository.GetListPaged(pageNumber, rowsPerPage, conditions, orderby, parameters).ToList();
        }

        public int RecordCount(string conditions, object parameters = null)
        {
            return _repository.RecordCount(conditions, parameters);
        }
    }
}